package standard

import (
	"go-forth/pkg/forth"
	"io/fs"
)

func SQuote2(vm forth.StackVM) {
	vm.Push(0) // STATE
	Fetch(vm)
	if vm.Pop() == vFalse {
		ccc := readTill(vm, "\"")
		vm.Push(ccc)
		vm.Push(len(ccc))
		return
	}
	SQuote(vm)
}

func OpenFile(vm forth.StackVM) {
	_ = vm.Pop().(int) // mod not used
	s := getString(vm)
	handle, err := vm.Syscall().OpenFile(s)
	vm.Push(handle)
	vm.Push(err)
}

func IncludeFile(vm forth.StackVM) {
	handle := vm.Pop().(int)
	f := vm.DataSpace().Fetch(handle).(fs.File)
	vm.SetInput(f)
}

func OpenDir(vm forth.StackVM) {
	s := getString(vm)
	handle, err := vm.Syscall().OpenDir(s)
	vm.Push(handle)
	vm.Push(err)
}

func ReadDir(vm forth.StackVM) {
	handle := vm.Pop().(int)
	u := vm.Pop().(int)
	buffer := vm.Pop().(int)
	u, flag, err := vm.Syscall().ReadDir(handle, buffer, u)
	vm.Push(u)
	vm.Push(flag)
	vm.Push(err)
}

func CloseDir(vm forth.StackVM) {
	handle := vm.Pop().(int)
	vm.Push(vm.Syscall().Close(handle))
}
